using System;
using NUnit.Framework;
using Inventory.BusinessEntities;
using System.Linq;
using DataAccess.UnitTests;
using DataAccess.BusinessEntities;

namespace Inventory.Tests
{
    [TestFixture]
    public class InventoryRequestTests : DataTestBase
    {
        [Test]
        public void TestRequest()
        {
            InventoryRequest request = new InventoryRequest();
            request.ProductId = 99;
            request.ProductIdType = ProductIdType.AllUsersProducts;
            request.StoreNumber = 45;
            request.StoreNumberType = StoreNumberType.AllUsersStores;
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var products = request.GetProductIdsToFind(user);
            Assert.AreEqual(3, products.Count());
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(3, stores.Count());
        }

        [Test]
        public void AllUsersProducts()
        {
            InventoryRequest request = new InventoryRequest();
            request.ProductId = 998; // A product that exists but is not in the user's list
            request.ProductIdType = ProductIdType.AllUsersProducts;
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var products = request.GetProductIdsToFind(user);
            Assert.AreEqual(3, products.Count());
            Assert.IsFalse(products.Contains(request.ProductId));
        }

        [Test]
        public void AllUsersStores()
        {
            InventoryRequest request = new InventoryRequest();
            request.StoreNumber = 998; // A store that exists but is not in the user's store list
            request.StoreNumberType = StoreNumberType.AllUsersStores;
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(3, stores.Count());
            Assert.IsFalse(stores.Contains(request.StoreNumber));
        }

        [Test]
        public void AllStores()
        {
            InventoryRequest request = new InventoryRequest();
            request.StoreNumber = 998; // A store that exists but is not in the user's stores
            request.StoreNumberType = StoreNumberType.AllStores;
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(5, stores.Count());
            Assert.IsTrue(stores.Contains(request.StoreNumber));
        }

        [Test]
        public void AllStoresWithStoreThatDoesntExist()
        {
            InventoryRequest request = new InventoryRequest();
            request.StoreNumber = 999; // A store that doesn't exist
            request.StoreNumberType = StoreNumberType.AllStores;
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(5, stores.Count());
            Assert.IsFalse(stores.Contains(request.StoreNumber));
        }

        [Test]
        public void SingleStore()
        {
            InventoryRequest request = new InventoryRequest();
            request.StoreNumber = 45; // A store that in the user's list
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(1, stores.Count());
            Assert.IsTrue(stores.Contains(request.StoreNumber));
        }

        [Test]
        public void SingleStoreThatDoesntExist()
        {
            InventoryRequest request = new InventoryRequest();
            request.StoreNumber = 999; // A store that doesn't exist
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var stores = request.GetStoreNumbersToFind(this.DomainContext, user);
            Assert.AreEqual(1, stores.Count());
            Assert.IsTrue(stores.Contains(request.StoreNumber));
        }

        [Test]
        public void SingleProduct()
        {
            InventoryRequest request = new InventoryRequest();
            request.ProductId = 99; // A product that is in the user's list
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var products = request.GetProductIdsToFind(user);
            Assert.AreEqual(1, products.Count());
            Assert.IsTrue(products.Contains(request.ProductId));
        }

        [Test]
        public void SingleProductNotInUsersList()
        {
            InventoryRequest request = new InventoryRequest();
            request.ProductId = 998; // A product that exists but is not in the user's list
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var products = request.GetProductIdsToFind(user);
            Assert.AreEqual(1, products.Count());
            Assert.IsTrue(products.Contains(request.ProductId));
        }

        [Test]
        public void SingleProductThatDoesntExist()
        {
            InventoryRequest request = new InventoryRequest();
            request.ProductId = 999; // A product that doesn't exist
            User user = this.DomainContext.Users.Single(u => u.Username == "jmclachl");
            var products = request.GetProductIdsToFind(user);
            Assert.AreEqual(1, products.Count());
            Assert.IsTrue(products.Contains(request.ProductId));
        }
    }
}
